package com.ifatter.http;

import android.content.Context;

import com.ifatter.http.HttpClient.HttpRequestListener;

public class API extends APISupport {

	public API(Context context) {
		super(context);
	}

	/**
	 * Issues an HTTP POST request.
	 * 
	 * @param url
	 *            the request url
	 * @param authenticate
	 *            if true, the request will be sent with BASIC authentication
	 *            header
	 * @return the response
	 * @throws HttpException
	 *             when JiKe service or network is unavailable
	 */
	public Response post(String url, PostParameter[] postParameters)
			throws HttpException {

		return post(url, postParameters, null, null);
	}

	/**
	 * Issues an HTTP POST request.
	 * 
	 * @param url
	 *            the request url
	 * @param authenticate
	 *            if true, the request will be sent with BASIC authentication
	 *            header
	 * @return the response
	 * @throws HttpException
	 *             when JiKe service or network is unavailable
	 */
	public Response post(String url, PostParameter[] postParameters,
			MultipartFile[] multFile, HttpRequestListener httpRequestListener)
			throws HttpException {

		return http.post(url, postParameters, multFile, httpRequestListener);
	}

	/**
	 * Issues an HTTP GET request.
	 * 
	 * @param url
	 *            the request url
	 * @param authenticate
	 *            if true, the request will be sent with BASIC authentication
	 *            header
	 * @return the response
	 * @throws HttpException
	 *             when JiKe service or network is unavailable
	 */

	public Response get(String url, boolean authenticate) throws HttpException {
		return get(url, null, authenticate);
	}

	/**
	 * Issues an HTTP GET request.
	 * 
	 * @param url
	 *            the request url
	 * @param params
	 *            the request parameters
	 * @param authenticate
	 *            if true, the request will be sent with BASIC authentication
	 *            header
	 * @return the response
	 * @throws HttpException
	 *             when JiKe service or network is unavailable
	 */
	protected Response get(String url, PostParameter[] params,
			boolean authenticate) throws HttpException {
		if ((null != params) && (params.length > 0)) {
			if (url.indexOf('?') == -1) {
				url += "?" + HttpClient.encodeParameters(params);
			} else {
				url += "&" + HttpClient.encodeParameters(params);
			}
		}
		return http.get(url);
	}

	/**
	 * Issues an HTTP DELETE request.
	 * 
	 * @param url
	 *            the request url
	 * @param params
	 *            the request parameters
	 * @param authenticate
	 *            if true, the request will be sent with BASIC authentication
	 *            header
	 * @return the response
	 * @throws HttpException
	 *             when JiKe service or network is unavailable
	 */
	protected Response delete(String url, PostParameter[] params)
			throws HttpException {
		if ((null != params) && (params.length > 0)) {
			if (url.indexOf('?') == -1) {
				url += "?" + HttpClient.encodeParameters(params);
			} else {
				url += "&" + HttpClient.encodeParameters(params);
			}
		}
		return http.delete(url);
	}

}
